package com.mz.ozy.activity.home;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jaeger.library.StatusBarUtil;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.luck.picture.lib.permissions.RxPermissions;
import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.activity.assets.ExchangeActivity;
import com.mz.ozy.activity.assets.RechargeActivity;
import com.mz.ozy.activity.assets.ScanHomeActivity;
import com.mz.ozy.activity.assets.TransferActivity;
import com.mz.ozy.activity.c2c.C2cTradeActivity;
import com.mz.ozy.activity.me.InviteCodeActivity;
import com.mz.ozy.activity.me.MsgActivity;
import com.mz.ozy.activity.me.MsgDetailActivity;
import com.mz.ozy.activity.shop.ShopDetailActivity;
import com.mz.ozy.adapter.BannerAdapter;
import com.mz.ozy.adapter.HomeCoinsAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.CheckBean;
import com.mz.ozy.bean.HomeBean;
import com.mz.ozy.bean.HomeMerBean;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.ObShopBean;
import com.mz.ozy.bean.UnreadMsgBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.HomePresenter;
import com.mz.ozy.mvp.view.HomeContract;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.MarqueeView;
import com.mz.ozy.widget.dialog.MsgDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 首页
 */
public class HomeActivity extends BaseActivity<HomeContract.View, HomePresenter> implements HomeContract.View, View.OnClickListener {

    @BindView(R.id.bannerPager)
    RollPagerView rollPagerView;
    @BindView(R.id.marqueeView)
    MarqueeView marqueeView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;


    @BindView(R.id.rele)
    RelativeLayout rele;

    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.ll_angel_exchange)
    ImageView llAngelExchange;
    @BindView(R.id.ll_save_edss)
    ImageView llSaveEdss;
    @BindView(R.id.image_home)
    ImageView image_home;

    private HomeCoinsAdapter mHomeAdapter;
    private BannerAdapter mBannerAdapter;
    private boolean isFirst = true;
    private List<HomeBean.BannerListBean> mBannerListBeans = new ArrayList<>();
    List<String> msgStrList = new ArrayList<>();
    List<HomeBean.MsgListBean> mMsgListBeans = new ArrayList<>();
    private HomeBean mHomeBean;
    private MsgDialog mMsgDialog;
    private String usdtBalance;

    public static double bl;
    public static int grade;
    private String html;
    private MeBean mMeBean;
    private String language;
    public static String invitationCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        StatusBarUtil.setTranslucentForImageViewInFragment(HomeActivity.this, 20, null);
        initTitle();
        setTitleBarMargin();
        initView();
//        mPresenter.checkUpdateVersion(this);
//        mPresenter.getNewHome(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mPresenter.getHomeInfo(this, isFirst);
//        mPresenter.getMeInfo(this);
        isFirst = false;
    }

    private void initView() {
        rightImage.setVisibility(View.VISIBLE);
        rightImage.setImageResource(R.mipmap.home_msg);
        language = SPUtils.getInstance(this).getString(Constants.SPName.LANGUAGE, "zhmsg");
        //根据语言显示对应的图标
        if (language.equals("zhmsg")) {
            tvBack.setImageResource(R.mipmap.lang_zh);
        } else {
            tvBack.setImageResource(R.mipmap.lang_en);
        }
        tvBack.setOnClickListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//把布局设为横行排列
        recyclerview.setLayoutManager(layoutManager);
        mHomeAdapter = new HomeCoinsAdapter(null);
        recyclerview.setAdapter(mHomeAdapter);
        recyclerview.setNestedScrollingEnabled(false);


        rollPagerView.setAnimationDurtion(500);
        rollPagerView.setHintView(new ColorPointHintView(this, getResources().getColor(R.color.c_90ff), getResources().getColor(R.color.white)));
        mBannerAdapter = new BannerAdapter(this, mBannerListBeans);
        rollPagerView.setAdapter(mBannerAdapter);
        marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
            @Override
            public void onItemClick(int position, TextView textView) {
                Intent intent = new Intent(HomeActivity.this, MsgDetailActivity.class);
                intent.putExtra("id", mMsgListBeans.get(position).getId());
                startActivity(intent);
            }
        });
        mHomeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                HomeBean.ProductlistBean goodId = mHomeAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.ll_item:
                        Intent intent = new Intent(HomeActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", goodId.getId() + "");
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
//        if (language.equals("zhmsg")) {
//            mPresenter.changeLanguage(this, "cnmsg");
//        } else if (language.equals("cnmsg")) {
//            mPresenter.changeLanguage(this, "zhmsg");
//        }
        if (TextUtils.isEmpty(language) || language.equals("zhmsg")) {
            SPUtils.getInstance(this).putString(Constants.SPName.LANGUAGE, "cnmsg");
        } else if (language.equals("cnmsg")) {
            SPUtils.getInstance(this).putString(Constants.SPName.LANGUAGE, "zhmsg");
        }
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }

    @OnClick({R.id.image_home, R.id.rightImage, R.id.tv_more, R.id.ll_save_edss, R.id.ll_angel_exchange, R.id.tv_with, R.id.tv_change, R.id.tv_transfer, R.id.tv_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_home:
                Intent intent0 = new Intent(this, InviteCodeActivity.class);
                intent0.putExtra("userlink", mMeBean.getUserLink());
                //  intent0.putExtra("code", mMeBean.getInvitationCode());
                startActivity(intent0);

                break;
            case R.id.rightImage:
                showMsgDialog();
                break;
            case R.id.tv_more:
                startActivity(new Intent(this, MsgActivity.class));
                break;
            case R.id.ll_save_edss:
                break;
            case R.id.ll_angel_exchange:
//                ToastUtils.showToast("暂未开放");
                Intent intentC2c = new Intent(this, C2cTradeActivity.class);
                startActivity(intentC2c);
                break;
            case R.id.tv_with:
                RechargeActivity.go(this, 0, false, "");
                break;
            case R.id.tv_change:
                Intent intent2 = new Intent(this, ExchangeActivity.class);
                intent2.putExtra("type", "0");
                startActivity(intent2);
                break;
            case R.id.tv_transfer:
                Intent intentTr = new Intent(this, TransferActivity.class);
                intentTr.putExtra("type", "5");
                startActivity(intentTr);
                break;
            case R.id.tv_scan:
                new RxPermissions(this).request(Manifest.permission.CAMERA)
                        .subscribe(granted -> {
                            if (granted) {
                                Intent intent4 = new Intent(HomeActivity.this, ScanHomeActivity.class);
                                //    intent4.putExtra("code", mMeBean.getInvitationCode());
                                startActivityForResult(intent4, 104);
                            } else {
                                Log.i("permissions", Manifest.permission.CAMERA + "：" + "获取失败");
                            }
                        });

                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if (event.equals(Constants.EventCode.REFRESH_DETAIL_LIST)) {
//            mPresenter.getHomeInfo(this, isFirst);
        }
    }


//    @Override
//    public void getHomeInfoSuccess(HomeBean bean) {
//
//        mHomeBean = bean;
//        GlideUtils.loadRoundImg(this, image_home, bean.getYqimg(), 4);
//        GlideUtils.loadRoundImg(this, llAngelExchange, bean.getC2cimg(), R.mipmap.home_c2c_back, 4);
//
//        mBannerListBeans.clear();
//        mMsgListBeans.clear();
//        msgStrList.clear();
//        final List<HomeBean.BannerListBean> bannerList = bean.getBannerList();
//        if (bannerList != null && bannerList.size() > 0) {
//            for (HomeBean.BannerListBean bannerListBean : bannerList) {
//                mBannerListBeans.add(bannerListBean);
//            }
//        } else {
//            rollPagerView.setVisibility(View.GONE);
//
//        }
//        mBannerAdapter.notifyDataSetChanged();
//        List<HomeBean.MsgListBean> msgListBeans = bean.getMsgList();
//        if (msgListBeans != null && msgListBeans.size() > 0) {
//            mMsgListBeans.addAll(msgListBeans);
//            for (int i = 0; i < msgListBeans.size(); i++) {
//                msgStrList.add(msgListBeans.get(i).getTitle());
//            }
//            marqueeView.startWithList(msgStrList);
//        }
//        /*// useravatar-用户头像 yxnumber-有效直推人数 tdnumber-有效团队人数 ztyj-直推业绩 tdyj-团队业绩 yxze-有效总额 syze-累计合约收益)
//        HomeBean.UserMap userMap = bean.getUserMap();
//        tvUsdtPrice.setText(Utils.decimalUtils(userMap.getPrice(), 2));
//        tvWpkPrice.setText(Utils.decimalUtils(userMap.getWpkPrice(), 2));*/
//
//        final List<HomeBean.ProductlistBean> reteList = bean.getProductlist();
//        if (reteList != null && reteList.size() > 0) {
//            mHomeAdapter.setNewData(reteList);
//        } else {
//            recyclerview.setVisibility(View.GONE);
//
//        }
//
//        int isRead = Integer.parseInt(bean.getIsRead());
//        if (isRead == 0) {
//            showMsgDialog();
//        }
//    }
//
//    @Override
//    public void changeLanguageSuccess() {
////        if (TextUtils.isEmpty(language) || language.equals("zhmsg")) {
////            SPUtils.getInstance(this).putString(Constants.SPName.LANGUAGE, "cnmsg");
////        } else if (language.equals("cnmsg")) {
////            SPUtils.getInstance(this).putString(Constants.SPName.LANGUAGE, "zhmsg");
////        }
////        finish();
////        startActivity(new Intent(this, MainActivity.class));
//    }

//    @Override
//    public void checkUpdateVersionSuccess(CheckBean json) {
//        if (json != null) {
//            int vnumber = json.getVersionMap().getVnumber();
//
//            if (vnumber > Utils.getVersionCode(this)) {
//                new CheckDialog(HomeActivity.this, json).show();
//
//            }
//        }
//    }
//
//    @Override
//    public void getNewSuccess(HomeBean bean) {
//        html = bean.getXszn();
//    }
//
//    @Override
//    public void getLotteryStateSuccess(LotteryStateBean bean) {
//    }
//
//    @Override
//    public void getgetMeInfoSuccess(MeBean bean) {
//        mMeBean = bean;
//        invitationCode = mMeBean.getInvitationCode();
//    }

    public void showMsgDialog() {
        HomeBean.MsgMapBean mapBean = mHomeBean.getMsgMap();
        if (mapBean != null && !TextUtils.isEmpty(mapBean.getTitle())) {
            if (mMsgDialog == null) {
                mMsgDialog = new MsgDialog(this, mapBean.getTitle(), mapBean.getContent());
            }
            mMsgDialog.show();
        } else {
            ToastUtils.showToast(getString(R.string.str_not_msg));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMsgDialog != null) {
            mMsgDialog.dismiss();
            mMsgDialog = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ((requestCode == 104 && resultCode == RESULT_OK)) {
            String address = data.getStringExtra("userName");
            SPUtils.getInstance(this).putString("address", address);
            Intent intent = new Intent(this, TransferActivity.class);

            startActivity(intent);

        }

    }

    @Override
    public void checkUpdateVersionSuccess(CheckBean o) {

    }

    @Override
    public void getMyMerSuccess(HomeMerBean o) {

    }

    @Override
    public void getOrSuccess(HomeMerBean o) {

    }

    @Override
    public void getHomeInfoSuccess(HomeMerBean homeMerBean) {

    }

    @Override
    public void exitLoginSuccess(JSONObject bean) {
    }

    @Override
    public void getUnreadMsgSuccess(UnreadMsgBean bean) {

    }

    @Override
    public void getProductSuccess(ObShopBean bean) {

    }
}
